package org.blade.personal.framework.orm;

import org.blade.personal.framework.components.metadata.TypeMetaData;
import org.blade.personal.framework.dsl.express.Expression;
import org.blade.personal.framework.orm.base.Attribute;
import org.blade.personal.framework.orm.base.FloatAlias;
import org.blade.personal.framework.orm.base.IntegerAlias;
import org.blade.personal.framework.orm.base.StringAlias;
import org.blade.personal.framework.orm.ext.TypeAliesExtendHandler;

/**
 * 类型别名工厂
 * @author ZGF
 *
 * @param <T>
 */
public class TypeAliasFactory<T> {

	private static TypeAliasFactory factory = null;
	
	/**
	 * 获取单例工厂
	 * @return
	 */
	public static TypeAliasFactory newInstance(){
		
		if(factory == null){
			factory = new TypeAliasFactory();
		}
		return factory;
	}
	
	private TypeAliasFactory(){
		
	}
	
	/**
	 * 生产类型别名类（表达式）
	 * @param attr 
	 * @param clause
	 * @param alias
	 * @param sqlAlise
	 * @param type 	
	 * @param expr 
	 * @return
	 */
	public TypeAlias create(Attribute<T> attr, String clause, String alias,
			String sqlAlise, String type, Expression expr){
		TypeAlias typeAlise = null;
		
		//拓展辅助器
		TypeAliesExtendHandler extendHandler = null;
	
		TypeMetaData<T> metaData = new TypeMetaData<T>(attr,  clause,  alias,
				 sqlAlise,  type,  expr);
		
		if( Float.class.getName().equals(type)){
			typeAlise =  new FloatAlias( (TypeMetaData<Float>)metaData);
		}else if( Integer.class.getName().equals(type)){
			typeAlise =  new IntegerAlias( (TypeMetaData<Integer>)metaData);
		}else if( String.class.getName().equals(type)){
			typeAlise =  new StringAlias( (TypeMetaData<String>)metaData);
		}else{
			typeAlise =  extendHandler.extend(type);
		}
		return typeAlise;
	}
	
	
}
